home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Tool Chest / Printing / STD File Saver 2.0 / Source / MyPDEF_4_HandlingDialogs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-08  |  23.1 KB  |  853 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Copyright 1991-1996 Apple Computer. All rights reserved.
  3. **
  4. **    You may incorporate this sample code into your applications without
  5. **    restriction, though the sample code has been provided "AS IS" and the
  6. **    responsibility for its operation is 100% yours.  However, what you are
  7. **    not permitted to do is to redistribute the source as "DSC Sample Code"
  8. **    after having made changes. If you're going to re-distribute the source,
  9. **    we require that you make it clear in the source that the code was
  10. **    descended from Apple Sample Code, but that you've made changes.
  11. */
  12.  
  13. #include <QuickDraw.h>
  14. #include <Dialogs.h>
  15. #include <Printing.h>
  16. #include <Resources.h>
  17. #include <Errors.h>
  18. #include <Memory.h>
  19. #include <TextUtils.h>
  20. #include <OSUtils.h>
  21.  
  22. #include "DriverTypes.h"
  23.  
  24. pascal void HandleStyleItems(TPPrDlg theDlg, short ItemHit);
  25. pascal void HandleJobItems(TPPrDlg theDlg, short ItemHit);
  26.  
  27. void SetDialogControlValue(DialogPtr theDialog, short theItem, short theValue)
  28. {
  29.     short theType;
  30.     Handle theHandle;
  31.     Rect theRect;
  32.  
  33.     GetDialogItem(theDialog, theItem, &theType, &theHandle, &theRect);
  34.     if (!theHandle) return;
  35.     SetCtlValue((ControlHandle)theHandle, theValue);
  36. }
  37.  
  38. short GetDialogControlValue(DialogPtr theDialog, short theItem)
  39. {
  40.     short theType;
  41.     Handle theHandle;
  42.     Rect theRect;
  43.  
  44.     GetDialogItem(theDialog, theItem, &theType, &theHandle, &theRect);
  45.     if (!theHandle) return -1;
  46.     return GetCtlValue((ControlHandle)theHandle);
  47. }
  48.  
  49. void SetDialogItemString(DialogPtr theDialog, short theItem, StringPtr theString)
  50. {
  51.     short theType;
  52.     Handle theHandle;
  53.     Rect theRect;
  54.  
  55.     GetDialogItem(theDialog, theItem, &theType, &theHandle, &theRect);
  56.     if (!theHandle) return;
  57.     SetIText(theHandle, theString);
  58. }
  59.  
  60. void GetDialogItemString(DialogPtr theDialog, short theItem, StringPtr theString)
  61. {
  62.     short theType;
  63.     Handle theHandle;
  64.     Rect theRect;
  65.  
  66.     GetDialogItem(theDialog, theItem, &theType, &theHandle, &theRect);
  67.     if (!theHandle) return;
  68.     GetIText(theHandle, theString);
  69. }
  70.  
  71. void SetDialogItemNumber(DialogPtr theDialog, short theItem, long theValue)
  72. {
  73.     short theType;
  74.     Handle theHandle;
  75.     Rect theRect;
  76.     Str255 theStr;
  77.  
  78.     GetDialogItem(theDialog, theItem, &theType, &theHandle, &theRect);
  79.     if (!theHandle) return;
  80.     NumToString(theValue, theStr);
  81.     SetIText(theHandle, theStr);
  82. }
  83.  
  84. long GetDialogItemNumber(DialogPtr theDialog, short theItem)
  85. {
  86.     Str255 theStr;
  87.     long theValue;
  88.  
  89.     GetDialogItemString(theDialog, theItem, theStr);
  90.     StringToNum(theStr, &theValue);
  91.     return theValue;
  92. }
  93.  
  94. Rect GetDialogItemRect(DialogPtr theDialog, short theItem)
  95. {
  96.     short theType;
  97.     Handle theHandle;
  98.     Rect theRect;
  99.  
  100.     GetDialogItem(theDialog, theItem, &theType, &theHandle, &theRect);
  101.     return theRect;
  102. }
  103.  
  104. void SelectDialogItemString(DialogPtr theDialog, short theItem)
  105. {
  106.     SelIText(theDialog, theItem, startSelectAll, endSelectAll);
  107. }
  108.  
  109. void InvertDialogItem(DialogPtr theDialog, short theItem)
  110. {
  111.     Rect    theRect;
  112.  
  113.     theRect = GetDialogItemRect(theDialog, theItem);
  114.     InvertRect(&theRect);
  115. }
  116.  
  117. void DialogPosition(DialogPtr theDialog)
  118. {
  119. #pragma unused(theDialog)
  120.     /* this is a no-op here, since we're specifying the swell
  121.     ** system 7 dialog positioning in the DLOG resources. If
  122.     ** you want to support System 6, or have a position other
  123.     ** than "Alert position, main screen", you'll need to flesh
  124.     ** out this function.
  125.     */
  126. }
  127.  
  128. Handle ReallyGetResource(ResType theType, short resID)
  129. {
  130.     Handle    theRes = GetResource(theType,resID);
  131.     if(!theRes) return 0;
  132.     LoadResource(theRes);
  133.     return theRes;
  134. }
  135.  
  136. void FrameDialogItem(DialogPtr theDialog, short theItem)
  137. {
  138.     GrafPtr    oldPort;
  139.     Rect    itsBox = GetDialogItemRect((DialogPtr)theDialog, theItem);
  140.  
  141.     GetPort(&oldPort);
  142.     SetPort((GrafPtr)theDialog);
  143.     PenSize(3, 3);
  144.     InsetRect(&itsBox, -4, -4);
  145.     {
  146.         short    radius = (itsBox.top - itsBox.bottom) / 2;
  147.         if (radius < 16) radius = 16;
  148.         FrameRoundRect(&itsBox, radius, radius);
  149.     }
  150.     SetPort(oldPort);
  151. }
  152.  
  153. pascal void FilePrintDefault(THPrint hPrint)
  154. {
  155.     // the default in is the resource PREC 0, and we also initialize some important fields 
  156.     THPrint theDefault;
  157.  
  158.     theDefault = (THPrint)ReallyGetResource('PREC', 0);
  159.     if (theDefault == NULL) {
  160.         setPrintErr(resNotFound);
  161.         return;
  162.     }
  163.     **hPrint = **theDefault;
  164.  
  165.     if (StripAddress((Ptr)*hPrint) != StripAddress((Ptr)*theDefault)) 
  166.         ReleaseResource((Handle)theDefault);
  167. }
  168.  
  169. pascal Boolean FilterNumPrStlAndJob(DialogPtr theDialog, EventRecord *theEvent, short *ItemHit)
  170. {
  171. #pragma unused(theDialog)
  172.     if ((theEvent->what == keyDown) || (theEvent->what == autoKey)) {
  173.         char    theChar;
  174.         theChar = theEvent->message & charCodeMask;
  175.         if ((theChar == enterKey) || (theChar == returnKey)) {
  176.             *ItemHit = printOkButton;
  177.             return true;
  178.         } else if ((theChar!=backspaceKey)&&(theChar!=tabKey)&&(!isdigit(theChar))) {
  179.             SysBeep(1);
  180.             *ItemHit = printInvalidItem;
  181.             return true;
  182.         }
  183.     }
  184.     return false;
  185. }
  186.  
  187. void StlHideCustomSizeItems(DialogPtr theDialog)
  188. {
  189.     short item;
  190.  
  191.     if (!theDialog) return;
  192.  
  193.     for(item = stlHorizTitleID; item <= stlPixelsButtonID; item++) 
  194.         HideDItem(theDialog, item);
  195. }
  196.  
  197. void StlShowCustomSizeItems(DialogPtr theDialog)
  198. {
  199.     short item;
  200.     
  201.     if (!theDialog) return;
  202.  
  203.     for (item = stlHorizTitleID; item <= stlPixelsButtonID; item++) 
  204.         ShowDItem(theDialog, item);
  205. }
  206.  
  207. pascal Boolean FilePrValidate(THPrint hPrint)
  208. {
  209.     // if the signature mySignature is present in the hPrint, then I
  210.     // consider that the values are ok else we just load anew the
  211.     // resource PREC 0
  212.     // this would be a bit more robust in a real driver
  213.  
  214.     THPrint theDefault;
  215.     Boolean changedIt = false;
  216.  
  217.     theDefault = (THPrint)ReallyGetResource('PREC', 0);
  218.     if (theDefault == NULL) {
  219.         DebugStr("\pCouldn't find the PREC 0 resource. That doesn't seem good.");
  220.         setPrintErr(resNotFound);
  221.         return false;
  222.     }
  223.     
  224.     if ((**hPrint).printX[7] != mySignature) {
  225.         **hPrint = **theDefault;
  226.         changedIt = true;
  227.     }
  228.  
  229.     ReleaseResource((Handle)theDefault);
  230.     
  231.     (**hPrint).prJob.iCopies = 1;
  232.     (**hPrint).prJob.pIdleProc = NULL;
  233.     (**hPrint).prJob.pFileName = NULL;
  234.     (**hPrint).prJob.iFileVol = 0;
  235.     (**hPrint).prJob.bFileVers = 0;
  236.     
  237.     return changedIt;
  238. }
  239.  
  240. pascal TPPrDlg FilePrStlInit(THPrint hPrint)
  241. {
  242.     // we initialize the dialog items with the right settings coming from hPrint 
  243.     TPPrDlg theDlg;
  244.     DialogPtr aDlg;
  245.     THPrint theDefault;
  246.  
  247.     theDefault = (THPrint)ReallyGetResource('PREC', 1);
  248.     if (theDefault != NULL) 
  249.         **hPrint = **theDefault;
  250.  
  251.     theDlg = (TPPrDlg)NewPtr(sizeof(TPrDlg));
  252.     if (theDlg == NULL) {
  253.         setPrintErr(iMemFullErr);
  254.         return 0;
  255.     }
  256.     
  257.     theDlg->pFltrProc = NewModalFilterProc(FilterNumPrStlAndJob);
  258.     theDlg->pItemProc = NewPItemProc(HandleStyleItems);
  259.     theDlg->fDoIt = false;
  260.     theDlg->fDone = false;
  261.     theDlg->hPrintUsr = hPrint;
  262.  
  263.     aDlg = GetNewDialog(printDialogID, theDlg, (WindowPtr)-1L);
  264.     if (aDlg == NULL) {
  265.         setPrintErr(resNotFound);
  266.         DisposePtr((Ptr)theDlg);
  267.         return 0;
  268.     }
  269.     DialogPosition(aDlg);
  270.  
  271.     switch((**hPrint).printX[0]) {
  272.         default:
  273.             DebugStr("\pBad value for paper size");
  274.             (**hPrint).printX[0] = printOnA4;
  275.             // fall through
  276.         case printOnA4:
  277.             SetDialogControlValue(aDlg, stlPaperA4SizeButtonID, 1);
  278.             SetDialogControlValue(aDlg, stlPaperLetterSizeButtonID, 0);
  279.             SetDialogControlValue(aDlg, stlPaperCustomSizeButtonID, 0);
  280.             StlHideCustomSizeItems(aDlg);
  281.             break;
  282.         case printOnLetter:
  283.             SetDialogControlValue(aDlg, stlPaperA4SizeButtonID, 0);
  284.             SetDialogControlValue(aDlg, stlPaperLetterSizeButtonID, 1);
  285.             SetDialogControlValue(aDlg, stlPaperCustomSizeButtonID, 0);
  286.             StlHideCustomSizeItems(aDlg);
  287.             break;
  288.         case printOnCustom:
  289.             SetDialogControlValue(aDlg, stlPaperA4SizeButtonID, 0);
  290.             SetDialogControlValue(aDlg, stlPaperLetterSizeButtonID, 0);
  291.             SetDialogControlValue(aDlg, stlPaperCustomSizeButtonID, 1);
  292.             StlShowCustomSizeItems(aDlg);
  293.             break;
  294.     }
  295.  
  296.     SetDialogItemNumber(aDlg, stlHorizTextID, (**hPrint).printX[2]);
  297.     SetDialogItemNumber(aDlg, stlVertTextID, (**hPrint).printX[3]);
  298.     switch((**hPrint).printX[4]) {
  299.         default:
  300.             DebugStr("\pBad value for units...repairing...");
  301.             (**hPrint).printX[4] = unitsMilliInch;
  302.             // fall through
  303.         case unitsMilliInch:
  304.             SetDialogControlValue(aDlg, stlMilliInchButtonID, 1);
  305.             SetDialogControlValue(aDlg, stlMilliMeterButtonID, 0);
  306.             SetDialogControlValue(aDlg, stlPixelsButtonID, 0);
  307.             break;
  308.         case unitsMilliMeter:
  309.             SetDialogControlValue(aDlg, stlMilliInchButtonID, 0);
  310.             SetDialogControlValue(aDlg, stlMilliMeterButtonID, 1);
  311.             SetDialogControlValue(aDlg, stlPixelsButtonID, 0);
  312.             break;
  313.         case unitsPixels:
  314.             SetDialogControlValue(aDlg, stlMilliInchButtonID, 0);
  315.             SetDialogControlValue(aDlg, stlMilliMeterButtonID, 0);
  316.             SetDialogControlValue(aDlg, stlPixelsButtonID, 1);
  317.             break;
  318.     }
  319.     ShowWindow(aDlg);
  320.     DrawDialog(aDlg);
  321.     SetPort(aDlg);
  322.     theDlg->lUser1 = (**hPrint).printX[5];
  323.     if ((**hPrint).printX[5])
  324.         InvertDialogItem(aDlg,stlLandscapeIconID);
  325.     else
  326.         InvertDialogItem(aDlg,stlPortraitIconID);
  327.     ValidRect(&(aDlg->portRect));
  328.     return theDlg;
  329. }
  330.  
  331. // redraw the frame around the ok button, and pay attention to the orientation icons 
  332. void RefreshDialog(TPPrDlg thePrDlg)
  333. {
  334.  
  335.     DrawDialog((DialogPtr)thePrDlg);
  336.     FrameDialogItem((DialogPtr)thePrDlg,printOkButton);
  337.  
  338.     if (thePrDlg->lUser1)
  339.         InvertDialogItem((DialogPtr)thePrDlg,stlLandscapeIconID);
  340.     else
  341.         InvertDialogItem((DialogPtr)thePrDlg,stlPortraitIconID);
  342.     ValidRect(&(((GrafPtr)thePrDlg)->portRect));
  343. }
  344.  
  345. void HandleStlHelp(void) {
  346.     short        item;
  347.     DialogPtr    helpDlg = GetNewDialog(helpDialogID, NULL, (WindowPtr)-1);
  348.  
  349.     if (!helpDlg) {
  350.         SysBeep(1);
  351.         return;
  352.     }
  353.  
  354.     for(item = 6; item <= 11; item++) 
  355.         HideDItem(helpDlg, item);
  356.  
  357.     ShowWindow(helpDlg);
  358.     DrawDialog(helpDlg);
  359.     SetPort((GrafPtr)helpDlg);
  360.     ValidRect(&(helpDlg->portRect));
  361.  
  362.     TextFont(1);
  363.     TextSize(9);
  364.  
  365.     for(item = 6; item <= 11; item++) 
  366.         ShowDItem(helpDlg, item);
  367.  
  368.     FrameDialogItem(helpDlg,1);
  369.     
  370.     while(item!=printOkButton)
  371.         ModalDialog(NULL, &item);
  372.  
  373.     DisposeDialog(helpDlg);
  374. }
  375.  
  376. // the paper sizes in the following routines come from the LaserWriter ppd
  377. // I've used the full-paper size as the size for the PICT. You'll probably
  378. // want to use something like the imageable area, but by making them this
  379. // size, it'll get you to look here to fix up the sizes. All the sizes
  380. // could be stored in resources (use the 'pgsz' maybe) as well, but this is
  381. // a simple sample.
  382. static void SetPaperA4(long *height, long *width, Boolean isLandscape)
  383. {
  384.     const short a4PaperWidth = 595;
  385.     const short a4PaperHeight = 842;
  386.  
  387.     if (!isLandscape) {
  388.         *width = a4PaperWidth;
  389.         *height = a4PaperHeight;
  390.     } else {
  391.         *width = a4PaperHeight;
  392.         *height = a4PaperWidth;
  393.     }
  394. }
  395.  
  396. static void SetPaperLetter(long *height, long *width, Boolean isLandscape)
  397. {
  398.     const short letterPaperWidth = 612;
  399.     const short letterPaperHeight = 792;
  400.  
  401.     if (!isLandscape) {
  402.         *width = letterPaperWidth;
  403.         *height = letterPaperHeight;
  404.     } else {
  405.         *width = letterPaperHeight;
  406.         *height = letterPaperWidth;
  407.     }
  408. }
  409.  
  410. static void SetPaperLegal(long *height, long *width, Boolean isLandscape)
  411. {
  412.     const short legalPaperWidth = 612;
  413.     const short legalPaperHeight = 1008;
  414.  
  415.     if (!isLandscape) {
  416.         *width = legalPaperWidth;
  417.         *height = legalPaperHeight;
  418.     } else {
  419.         *width = legalPaperHeight;
  420.         *height = legalPaperWidth;
  421.     }
  422. }
  423.  
  424. static void SetPaperCustom(long *height, long *width, Boolean isLandscape,
  425.     short units, long rawHeight, long rawWidth)
  426. {
  427.     float    scaleFactor;
  428.     long    scaleWidth,scaleHeight;
  429.  
  430.     switch (units) {
  431.         case unitsMilliInch:    scaleFactor = 0.072;        break;
  432.         case unitsMilliMeter:    scaleFactor = 720.0/254;    break;
  433.         case unitsPixels:        scaleFactor = 1.0;            break;
  434.         default:                scaleFactor = 1.0;            break;    // have to set it to SOMETHING
  435.     }
  436.     
  437.     scaleWidth = rawWidth * scaleFactor;
  438.     scaleHeight = rawHeight * scaleFactor;
  439.  
  440.     if (!isLandscape) {
  441.         *width = rawWidth;
  442.         *height = rawHeight;
  443.     } else {
  444.         *width = rawHeight;
  445.         *height = rawWidth;
  446.     }
  447. }
  448.  
  449. static Boolean SetPaperSize(THPrint hPrintUsr)
  450. {
  451.     long    height=0,width=0;
  452.  
  453.     switch((**hPrintUsr).printX[0]) {
  454.         case printOnA4:
  455.             SetPaperA4(&height,&width,(**hPrintUsr).printX[5]);
  456.             break;
  457.         case printOnLetter:
  458.             SetPaperLetter(&height,&width,(**hPrintUsr).printX[5]);
  459.             break;
  460.         case printOnCustom:
  461.             SetPaperCustom(&height,&width,(**hPrintUsr).printX[5],
  462.                 (**hPrintUsr).printX[4],(**hPrintUsr).printX[3],(**hPrintUsr).printX[2]);
  463.             break;
  464.         case printOnLegal:
  465.             SetPaperLegal(&height,&width,(**hPrintUsr).printX[5]);
  466.             break;
  467.         default:
  468.             return false;
  469.             break;
  470.     }
  471.     
  472.     SetRect(&((**hPrintUsr).rPaper), 0, 0, width, height);
  473.     (**hPrintUsr).prInfo.rPage = (**hPrintUsr).rPaper;
  474.     (**hPrintUsr).prInfoPT.rPage = (**hPrintUsr).rPaper;
  475.     (**hPrintUsr).prStl.iPageV = (height * 120) / 72;
  476.     (**hPrintUsr).prStl.iPageH = (width * 120) / 72;
  477.     return true;
  478. }
  479.  
  480. pascal void HandleStyleItems(TPPrDlg thePrDlg, short ItemHit)
  481. {
  482.     THPrint hPrintUsr = thePrDlg->hPrintUsr;
  483.     DialogPtr    theDialog = (DialogPtr)(thePrDlg);
  484.  
  485.     switch (ItemHit) { 
  486.         case printOkButton:
  487.             if (GetDialogControlValue(theDialog, stlPaperA4SizeButtonID) == 1) {
  488.                 (**hPrintUsr).printX[0] = printOnA4;
  489.                 (**hPrintUsr).printX[2] = 0;
  490.                 (**hPrintUsr).printX[3] = 0;
  491.                 (**hPrintUsr).printX[4] = unitsUndefined;
  492.             } else if (GetDialogControlValue(theDialog, stlPaperLetterSizeButtonID) == 1) {
  493.                 (**hPrintUsr).printX[0] = printOnLetter;
  494.                 (**hPrintUsr).printX[2] = 0;
  495.                 (**hPrintUsr).printX[3] = 0;
  496.                 (**hPrintUsr).printX[4] = unitsUndefined;
  497.             } else if (GetDialogControlValue(theDialog, stlPaperCustomSizeButtonID) == 1) {
  498.                 (**hPrintUsr).printX[0] = printOnCustom;
  499.                 if (GetDialogControlValue(theDialog, stlMilliInchButtonID) == 1)
  500.                     (**hPrintUsr).printX[4] = unitsMilliInch;
  501.                 if (GetDialogControlValue(theDialog, stlMilliMeterButtonID) == 1)
  502.                     (**hPrintUsr).printX[4] = unitsMilliMeter;
  503.                 if (GetDialogControlValue(theDialog, stlPixelsButtonID) == 1)
  504.                     (**hPrintUsr).printX[4] = unitsPixels;
  505.     
  506.                 (**hPrintUsr).printX[2] = GetDialogItemNumber(theDialog, stlHorizTextID);
  507.                 (**hPrintUsr).printX[3] = GetDialogItemNumber(theDialog, stlVertTextID);
  508.             } else {
  509.                 (**hPrintUsr).printX[0] = printOnUndefined;
  510.                 (**hPrintUsr).printX[2] = 0;
  511.                 (**hPrintUsr).printX[3] = 0;
  512.                 (**hPrintUsr).printX[4] = unitsUndefined;
  513.             }
  514.  
  515.             (**hPrintUsr).printX[5] = (thePrDlg->lUser1 != 0);
  516.  
  517.             if (SetPaperSize(hPrintUsr)) {
  518.                 thePrDlg->fDone = true;
  519.                 thePrDlg->fDoIt = true;
  520.             } else {
  521.                 NoteAlert(badValueAlertID,nil);
  522.             }
  523.             break;
  524.         case printCancelButton:
  525.             thePrDlg->fDone = true;
  526.             thePrDlg->fDoIt = false;
  527.             break;
  528.         case stlPaperA4SizeButtonID:
  529.             if (GetDialogControlValue(theDialog, ItemHit) == 0) {
  530.                 if (GetDialogControlValue(theDialog, stlPaperCustomSizeButtonID)) 
  531.                     StlHideCustomSizeItems(theDialog);
  532.                 SetDialogControlValue(theDialog, stlPaperCustomSizeButtonID, 0);
  533.                 SetDialogControlValue(theDialog, stlPaperLetterSizeButtonID, 0);
  534.                 SetDialogControlValue(theDialog, stlPaperA4SizeButtonID, 1);
  535.             }
  536.             break;
  537.         case stlPaperLetterSizeButtonID:
  538.             if (GetDialogControlValue(theDialog, ItemHit) == 0) {
  539.                 if (GetDialogControlValue(theDialog, stlPaperCustomSizeButtonID)) 
  540.                     StlHideCustomSizeItems(theDialog);
  541.                 SetDialogControlValue(theDialog, stlPaperCustomSizeButtonID, 0);
  542.                 SetDialogControlValue(theDialog, stlPaperLetterSizeButtonID, 1);
  543.                 SetDialogControlValue(theDialog, stlPaperA4SizeButtonID, 0);
  544.             }
  545.             break;
  546.         case stlPaperCustomSizeButtonID:
  547.             if (GetDialogControlValue(theDialog, ItemHit) == 0) {
  548.                 StlShowCustomSizeItems(theDialog);
  549.                 SetDialogControlValue(theDialog, stlPaperCustomSizeButtonID, 1);
  550.                 SetDialogControlValue(theDialog, stlPaperLetterSizeButtonID, 0);
  551.                 SetDialogControlValue(theDialog, stlPaperA4SizeButtonID, 0);
  552.             }
  553.             break;
  554.         case stlPortraitIconID:
  555.             if (thePrDlg->lUser1) {
  556.                 InvertDialogItem(theDialog, stlPortraitIconID);
  557.                 InvertDialogItem(theDialog, stlLandscapeIconID);
  558.                 thePrDlg->lUser1 = false;
  559.             }
  560.             break;
  561.         case stlLandscapeIconID:
  562.             if (!thePrDlg->lUser1) {
  563.                 InvertDialogItem(theDialog, stlPortraitIconID);
  564.                 InvertDialogItem(theDialog, stlLandscapeIconID);
  565.                 thePrDlg->lUser1 = true;
  566.             }
  567.         case stlMilliInchButtonID:
  568.         case stlMilliMeterButtonID: 
  569.         case stlPixelsButtonID: 
  570.             if (GetDialogControlValue(theDialog, ItemHit) == 0) {
  571.                 SetDialogControlValue(theDialog, stlMilliInchButtonID, 0);
  572.                 SetDialogControlValue(theDialog, stlMilliMeterButtonID, 0);
  573.                 SetDialogControlValue(theDialog, stlPixelsButtonID, 0);
  574.                 SetDialogControlValue(theDialog, ItemHit, 1);
  575.             }
  576.             break;
  577.         case stlHelpButtonID:
  578.             HandleStlHelp();
  579.             SetPort((GrafPtr)thePrDlg);
  580.             RefreshDialog(thePrDlg);
  581.             break;
  582.     }
  583. }
  584.  
  585. Boolean FilePrDlgStl(THPrint hPrint)
  586. {
  587.     TPPrDlg thePrDlg;
  588.     GrafPtr SavePort;
  589.     short itemHit;
  590.     THPrint theDefault;
  591.     Boolean doit = false;
  592.  
  593.     thePrDlg = FilePrStlInit(hPrint);
  594.     if (!thePrDlg) {
  595.         SysBeep(1);
  596.         return(false);
  597.     }
  598.  
  599.     GetPort(&SavePort);
  600.     FrameDialogItem((DialogPtr)thePrDlg, 1);
  601.     while (!(thePrDlg->fDone)) {
  602.         ModalDialog(thePrDlg->pFltrProc, &itemHit);
  603.         CallPItemProc(thePrDlg->pItemProc,(DialogPtr)thePrDlg, itemHit);
  604.     }
  605.  
  606.     SetPort(SavePort);
  607.     if (thePrDlg->fDoIt) {                // save in PREC 1 
  608.         theDefault = (THPrint)ReallyGetResource('PREC', 1);
  609.         if (theDefault != NULL) {
  610.             **theDefault = **hPrint;
  611.             ChangedResource((Handle)theDefault);
  612.             WriteResource((Handle)theDefault);
  613.         }
  614.         doit = true;
  615.     }
  616.  
  617.     DisposeRoutineDescriptor(thePrDlg->pFltrProc);
  618.     DisposeRoutineDescriptor(thePrDlg->pItemProc);
  619.     CloseDialog((DialogPtr)thePrDlg);
  620.     DisposePtr((Ptr)thePrDlg);
  621.  
  622.     return doit;
  623. }
  624.  
  625. pascal TPPrDlg FilePrJobInit(THPrint hPrint)
  626. {
  627.     // we initialize the dialog items with the right settings coming from hPrint 
  628.     DialogPtr aDlg;
  629.     TPPrDlg theDlg;
  630.     SysEnvRec theWorld;
  631.  
  632.     theDlg = (TPPrDlg) NewPtr(sizeof(TPrDlg));
  633.     if (theDlg == NULL) {
  634.         DebugStr("\pCouldn't allocate enough memory for a TPPrDlg. Ummerbay.");
  635.         setPrintErr(iMemFullErr);
  636.         return 0;
  637.     }
  638.  
  639.     theDlg->pFltrProc = NewModalFilterProc(FilterNumPrStlAndJob);
  640.     theDlg->pItemProc = NewPItemProc(HandleJobItems);
  641.     theDlg->fDoIt = false;
  642.     theDlg->fDone = false;
  643.     theDlg->hPrintUsr = hPrint;
  644.  
  645.     aDlg = GetNewDialog(pageSetupDialogID, theDlg, (WindowPtr)-1);
  646.     if (aDlg == NULL) {
  647.         DebugStr("\pCouldn't load the Page Setup dialog. Ummerbay.");
  648.         setPrintErr(resNotFound);
  649.         DisposePtr((Ptr)theDlg);
  650.         return 0;
  651.     }
  652.     DialogPosition(aDlg);
  653.  
  654.     SysEnvirons(1, &theWorld);
  655.  
  656.     if (!theWorld.hasColorQD) {
  657.         HideDItem(aDlg, jobColorButtonID);
  658.         HideDItem(aDlg, jobBlackWhiteButtonID);
  659.         (**hPrint).printX[8] = 1;
  660.     }
  661.  
  662.     if ((**hPrint).printX[6]) {
  663.         SetDialogItemNumber(aDlg, jobFromTextID, (**hPrint).prJob.iFstPage);
  664.         SetDialogItemNumber(aDlg, jobToTextID, (**hPrint).prJob.iLstPage);
  665.         SetDialogControlValue(aDlg, jobAllButtonID, 0);
  666.         SetDialogControlValue(aDlg, jobFromButtonID, 1);
  667.     } else {
  668.         SetDialogControlValue(aDlg, jobAllButtonID, 1);
  669.         SetDialogControlValue(aDlg, jobFromButtonID, 0);
  670.     }
  671.  
  672.     SetDialogControlValue(aDlg, jobColorButtonID, (**hPrint).printX[8] == 0);
  673.     SetDialogControlValue(aDlg, jobBlackWhiteButtonID, (**hPrint).printX[8] == 1);
  674.  
  675.     ShowWindow(aDlg);
  676.     SetPort(aDlg);
  677.     return theDlg;
  678. }
  679.  
  680. pascal void HandleJobItems(TPPrDlg thePrDlg, short ItemHit)
  681. {
  682.     THPrint hPrint = thePrDlg->hPrintUsr;
  683.     DialogPtr theDialog = (DialogPtr)(thePrDlg);
  684.     switch (ItemHit) { 
  685.         case printOkButton:
  686.             if (GetDialogControlValue(theDialog, jobAllButtonID) == 0) {
  687.                 (**hPrint).prJob.iFstPage = GetDialogItemNumber(theDialog, jobFromTextID);
  688.                 (**hPrint).prJob.iLstPage = GetDialogItemNumber(theDialog, jobToTextID);
  689.                 (**hPrint).printX[6] = 1;
  690.             } else {
  691.                 (**hPrint).prJob.iFstPage = 1;
  692.                 (**hPrint).prJob.iLstPage = 9999;
  693.                 (**hPrint).printX[6] = 0;
  694.             }
  695.  
  696.             // We don't handle copies at the moment, but if we did,
  697.             // and we were going to be the one to do multiple copies
  698.             // instead of letting the application do it, this'd be
  699.             // the place where we'd want to stuff the number of copies
  700.             // into a private structure, and put 1 in for the app.
  701.             // If we want the app to handle it, we should fill in the
  702.             // iCopies field of the job record here.
  703.  
  704.             if (GetDialogControlValue(theDialog, jobColorButtonID) == 0) 
  705.                 (**hPrint).printX[8] = 1;
  706.             else 
  707.                 (**hPrint).printX[8] = 0;
  708.  
  709.             thePrDlg->fDone = true;
  710.             thePrDlg->fDoIt = true;
  711.             break;
  712.  
  713.         case printCancelButton:
  714.             thePrDlg->fDone = true;
  715.             thePrDlg->fDoIt = false;
  716.             break;
  717.  
  718.         case jobFromButtonID:
  719.                 SetDialogItemNumber(theDialog, jobFromTextID, (**hPrint).prJob.iFstPage);
  720.                 SetDialogItemNumber(theDialog, jobToTextID, (**hPrint).prJob.iLstPage);
  721.         case jobAllButtonID:
  722.             if (GetDialogControlValue(theDialog, ItemHit) == 0) {
  723.                 SetDialogControlValue(theDialog, ItemHit, 1);
  724.                 SetDialogControlValue(theDialog,
  725.                     (jobAllButtonID+jobFromButtonID) - ItemHit, 0);    // change the other one
  726.             }
  727.             break;
  728.  
  729.         case jobFromTextID:
  730.         case jobToTextID:
  731.             if (GetDialogControlValue(theDialog, jobFromButtonID) == 0) {
  732.                 SetDialogItemNumber(theDialog, jobFromTextID, (**hPrint).prJob.iFstPage);
  733.                 SetDialogItemNumber(theDialog, jobToTextID, (**hPrint).prJob.iLstPage);
  734.                 SetDialogControlValue(theDialog, jobFromButtonID, 1);
  735.                 SetDialogControlValue(theDialog, jobAllButtonID, 0);
  736.             }
  737.             break;
  738.  
  739.         case jobColorButtonID:
  740.         case jobBlackWhiteButtonID:
  741.             if (GetDialogControlValue(theDialog, ItemHit) == 0) {
  742.                 SetDialogControlValue(theDialog, ItemHit, 1);
  743.                 SetDialogControlValue(theDialog,
  744.                     (jobColorButtonID + jobBlackWhiteButtonID) - ItemHit, 0);
  745.             }
  746.             break;
  747.     }
  748. }
  749.  
  750. Boolean FilePrDlgJob(THPrint hPrint)
  751. {
  752.     TPPrDlg thePrDlg;
  753.     GrafPtr SavePort;
  754.     short itemHit;
  755.     Boolean doit = false;
  756.  
  757.     thePrDlg = FilePrJobInit(hPrint);
  758.     if (!thePrDlg) {
  759.         SysBeep(1);
  760.         return(false);
  761.     }
  762.  
  763.     GetPort(&SavePort);
  764.     FrameDialogItem((DialogPtr)thePrDlg,1);
  765.     while (!(thePrDlg->fDone)) {
  766.         ModalDialog(thePrDlg->pFltrProc, &itemHit);
  767.         CallPItemProc(thePrDlg->pItemProc,(DialogPtr)thePrDlg, itemHit);
  768.     }
  769.  
  770.     SetPort(SavePort);
  771.     if (thePrDlg->fDoIt) {
  772.         THPrint theDefault;
  773.  
  774.         theDefault = (THPrint)ReallyGetResource('PREC', 1);
  775.         if (theDefault != NULL) {
  776.             **theDefault = **hPrint;
  777.             ChangedResource((Handle)theDefault);
  778.             WriteResource((Handle)theDefault);
  779.         }
  780.         doit = true;
  781.     }
  782.     DisposeRoutineDescriptor(thePrDlg->pFltrProc);
  783.     DisposeRoutineDescriptor(thePrDlg->pItemProc);
  784.     CloseDialog((DialogPtr)thePrDlg);
  785.     DisposePtr((Ptr)thePrDlg);
  786.  
  787.     return doit;
  788. }
  789.  
  790. // the main 4 entry points follow
  791. pascal Boolean FilePrStlDialog(THPrint hPrint)
  792. {
  793.     return FilePrDlgStl(hPrint);
  794. }
  795.  
  796. pascal Boolean FilePrJobDialog(THPrint hPrint)
  797. {
  798.     return FilePrDlgJob(hPrint);
  799. }
  800.  
  801. pascal void FilePrJobMerge(THPrint hPrintSrc, THPrint hPrintDst)
  802. {
  803.     FilePrValidate(hPrintSrc);
  804.     if (FilePrValidate(hPrintDst)) {    // something changed => it wasn't kosher
  805.         **hPrintDst = **hPrintSrc;        // so just slam in the "good" stuff
  806.     } else {
  807.         (**hPrintDst).prJob = (**hPrintSrc).prJob;    // this is all job specific
  808.         // printX[6] is flag saying whether we're doing a range or all pages
  809.         (**hPrintDst).printX[6] = (**hPrintSrc).printX[6];
  810.     }
  811.     FilePrValidate(hPrintDst);
  812. }
  813.  
  814. pascal Boolean FilePrDlgMain(THPrint hPrint, PDlgInitProcPtr initFunc)
  815. {
  816.     TPPrDlg thePrDlg;
  817.     GrafPtr SavePort;
  818.     short itemHit;
  819.     Boolean doit = false;
  820.  
  821.     thePrDlg = CallPDlgInitProc(initFunc,hPrint);
  822.     if (!thePrDlg) {
  823.         SysBeep(1);
  824.         return(false);
  825.     }
  826.  
  827.     GetPort(&SavePort);
  828.     FrameDialogItem((DialogPtr)thePrDlg,1);
  829.     while (!(thePrDlg->fDone)) {
  830.         ModalDialog(thePrDlg->pFltrProc, &itemHit);
  831.         CallPItemProc(thePrDlg->pItemProc,(DialogPtr)thePrDlg, itemHit);
  832.     }
  833.  
  834.     SetPort(SavePort);
  835.     if (thePrDlg->fDoIt) {
  836.         THPrint theDefault;
  837.  
  838.         theDefault = (THPrint)ReallyGetResource('PREC', 1);
  839.         if (theDefault != NULL) {
  840.             **theDefault = **hPrint;
  841.             ChangedResource((Handle)theDefault);
  842.             WriteResource((Handle)theDefault);
  843.         }
  844.         doit = true;
  845.     }
  846.     DisposeRoutineDescriptor(thePrDlg->pFltrProc);
  847.     DisposeRoutineDescriptor(thePrDlg->pItemProc);
  848.     CloseDialog((DialogPtr)thePrDlg);
  849.     DisposePtr((Ptr)thePrDlg);
  850.  
  851.     return doit;
  852. }
  853.